# -*- coding: utf-8 -*-
from graphs.listgraph import *
from graphs.matrixgraph import *
from graphs.generator import *
from timeit import Timer


class Main():

    def __init__(self):
        self.repeat = 5

    def log(self, message):
        print message

    def measure(self):
        start = 500
        delta = 100
        num = 1
        end = start + (num - 1) * delta

        outputname = 'results.dense.' + str(start) + '.'
        outputname += str(end) + '.txt'

        self.logfile = open(outputname, 'w')

        filenames = []

        for i in range(num):
            vertices = start + i * delta
            filename = 'inputs/dense.' + str(vertices) + '.txt'
            filenames.append(filename)

        for i in range(num):
            vertices = (i + 1) * delta
            filename = 'inputs/sparse.' + str(vertices) + '.txt'
            filenames.append(filename)

        count = start

        for filename in filenames:
            output = str(count) + ';'
            count += delta

            self.log('-------------')
            self.log(filename)
            self.log('-------------')

            # MatrixGraph - ListGraph - Dijkstra
            graph_impl = 'MatrixGraph'
            time = self.time_dijkstra(graph_impl, filename)
            self.log(graph_impl + ' - dijkstra:' + str(time))
            output += str(time) + ';'

            graph_impl = 'ListGraph'
            time = self.time_dijkstra(graph_impl, filename)
            self.log(graph_impl + ' - dijkstra:' + str(time))
            output += str(time) + ';'

            # MatrixGraph - ListGraph - Prim
            graph_impl = 'MatrixGraph'
            time = self.time_prim(graph_impl, filename)
            self.log(graph_impl + ' - prim:' + str(time))
            output += str(time) + ';'

            graph_impl = 'ListGraph'
            time = self.time_prim(graph_impl, filename)
            self.log(graph_impl + ' - prim:' + str(time))
            output += str(time) + ';'

            # MatrixGraph - ListGraph - Kruskal
            graph_impl = 'MatrixGraph'
            time = self.time_kruskal(graph_impl, filename)
            self.log(graph_impl + ' - kruskal:' + str(time))
            output += str(time) + ';'

            graph_impl = 'ListGraph'
            time = self.time_kruskal(graph_impl, filename)
            self.log(graph_impl + ' - kruskal:' + str(time))
            output += str(time) + ';'

            output = output.replace('.', ',')
            self.logfile.write(output + '\n')

        self.logfile.close()

    def time_dijkstra(self, implementation, filename):
        setup = self.get_setup(implementation, filename)
        setup += "index = graph.vertex_count() - 1\n"
        setup += "destination = 'V' + str(index)\n"

        code = "graph.run_dijkstra('V0', destination)"

        return self.time(code, setup)

    def time_kruskal(self, implementation, filename):
        setup = self.get_setup(implementation, filename)

        code = "graph.run_kruskal()"

        return self.time(code, setup)

    def time_prim(self, implementation, filename):
        setup = self.get_setup(implementation, filename)

        code = "graph.run_prim()"

        return self.time(code, setup)

    def get_setup(self, implementation, filename):
        setup = "from graphs." + implementation.lower()
        setup += " import " + implementation + "\n"
        setup += "graph = " + implementation + "()\n"
        setup += "graph.load('" + filename + "')\n"

        return setup

    def time(self, code, setup):
        timer = Timer(code, setup)

        return (timer.timeit(self.repeat) / self.repeat)

    def generate_graphs(self):
        gen = Generator()

        densefactor = 0.5
        sparsefactor = 0.01

        vertices = 50
        delta = 50

        for i in range(50):
            print 'Generating graphs with', vertices, 'vertices...'

            filename = 'inputs/dense.' + str(vertices) + '.txt'
            gen.generate(vertices, densefactor, filename)

            filename = 'inputs/sparse.' + str(vertices) + '.txt'
            gen.generate(vertices, sparsefactor, filename)

            vertices += delta

    def list_vs_array(self):
        repeat = 100
        setup = "from structures.list import List\n"
        setup += "list = List()\n"
        setup += "for i in range(100000):\n"
        setup += "    list.add(i)\n"

        code = "for i in list:\n"
        code += "    pass"
        timer = Timer(code, setup)

        print 'list:', (timer.timeit(repeat) / repeat)

        setup = "list = []\n"
        setup += "for i in range(100000):\n"
        setup += "    list.append(i)\n"

        code = "for i in list:\n"
        code += "    pass"
        timer = Timer(code, setup)

        print 'array:', (timer.timeit(repeat) / repeat)

    def main(self):
        self.measure()
#        self.list_vs_array()
#        self.generate_graphs()


if __name__ == '__main__':
    main = Main()
    main.main()
